<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>我的任务</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-select
          clearable
          size="small"
          v-model="formInline.state"
          placeholder="状态"
        >
          <el-option
            v-for="item in this.states"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          size="small"
          v-model="formInline.applyid"
          placeholder="输入申请单"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-printer"
          v-print="'#pdfDom'"
          >打印</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      id="pdfDom"
      size="small"
      :data="applyData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        sortable
        prop="applicationid"
        label="申请单号"
        width="180"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="type"
        label="申请类型"
        width="110"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="state"
        label="状态"
        width="100"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="applicant"
        label="申请人"
        min-width="160"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="applyremark"
        label="申请人备注"
        min-width="160"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        :default-sort="{ prop: 'applytime', order: 'descending' }"
        prop="applytime"
        label="申请时间"
        min-width="100"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="reviewer"
        label="审核人"
        min-width="120"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="checker"
        label="验收人"
        min-width="120"
      >
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="230">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >详情</el-button
          >
          <el-button
            :disabled="getdisabled(scope.row.state)"
            size="mini"
            type="success"
            @click="submit('通过', scope.$index, scope.row)"
            >通过</el-button
          >
          <el-button
            :disabled="getdisabled(scope.row.state)"
            size="mini"
            type="warning"
            @click="submit('不通过', scope.$index, scope.row)"
            >不通过</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 查看界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="60%"
      @click="closeDialog('edit')"
    >
      <!-- 申请单概述 -->
      <el-descriptions
        class="margin-top"
        title="申请单概述"
        :column="3"
        :size="size"
        border
      >
        <!-- 单号applicationid 类型type 总价值price -->
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            申请单号
          </template>
          {{ this.details.applicationid }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            类型
          </template>
          {{ this.details.type }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            总价值
          </template>
          {{ this.details.price }}
        </el-descriptions-item>
        <!-- 转入仓库instore 转出仓库outstore 当前状态state  -->
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            {{ this.getinstorestr(this.details.type) }}
          </template>
          {{ this.details.instore }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            {{ this.getoutstorestr(this.details.type) }}
          </template>
          {{ this.details.outstore }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            当前状态
          </template>
          {{ this.details.state }}
        </el-descriptions-item>
      </el-descriptions>
      <!-- 流程详情 -->
      <br />
      <el-descriptions
        class="margin-top"
        title="流程详情"
        :column="4"
        :size="size"
        border
      >
        <!-- 申请人applicantid 申请时间applytime 申请备注applyremark 留空 -->
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            申请人
          </template>
          {{ this.details.applicantid }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            申请时间
          </template>
          {{ this.details.applytime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            申请备注
          </template>
          {{ this.details.applyremark }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> </template>
        </el-descriptions-item>
        <!-- 审核人reviewerid 审核意见reviewpass 审核时间reviewtime 申请备注reviewremark -->
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            审核人
          </template>
          {{ this.details.reviewerid }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            审核意见
          </template>
          {{ this.details.reviewpass }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            审核时间
          </template>
          {{ this.details.reviewtime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            申请备注
          </template>
          {{ this.details.reviewremark }}
        </el-descriptions-item>
        <!-- 验收人checkerid 验收意见checkpass 验收时间checktime 验收备注checkremark -->
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            验收人
          </template>
          {{ this.details.checkerid }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            验收意见
          </template>
          {{ this.details.checkpass }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            验收时间
          </template>
          {{ this.details.checktime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            验收备注
          </template>
          {{ this.details.checkremark }}
        </el-descriptions-item>
      </el-descriptions>
      <!-- 申请单材料详情 -->
      <br />
      <el-descriptions
        class="margin-top"
        title="材料详情"
        :column="4"
        :size="size"
        border
      >
        <!-- 材料种类material 材料名称mname 数量quantity 价值price -->
        <template v-for="(item, index) in this.details.materials">
          <el-descriptions-item label="材料种类" :key="index">{{
            item.tname
          }}</el-descriptions-item>
          <el-descriptions-item label="材料名称" :key="index">{{
            item.mname
          }}</el-descriptions-item>
          <el-descriptions-item label="数量" :key="index">{{
            item.quantity
          }}</el-descriptions-item>
          <el-descriptions-item label="价值" :key="index">{{
            item.price
          }}</el-descriptions-item>
        </template>
      </el-descriptions>
      <!-- 审批栏 -->
      <br />
      <div v-show="this.details.state != '验收完成'">
      <el-descriptions class="margin-top" title="审批意见" border>
      </el-descriptions>
      <el-form
        label-width="80px"
        ref="editForm"
        :model="editForm"
        :rules="rules"
      >
        <el-form-item label="是否通过" prop="opinion">
          <el-radio-group v-model="editForm.opinion">
            <el-radio label="通过"></el-radio>
            <el-radio label="不通过"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input
            type="textarea"
            size="small"
            v-model="editForm.notes"
            placeholder="可输入审批或验收备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">关闭</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >提交</el-button
        >
      </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import { getmytask, examine, applydetails } from "../../api/userMG";
import { getStateItem } from "../../api/basisMG";
import { getCookie } from "../../utils/util";
import Pagination from "../../components/Pagination";
import print from "vue-print-nb";
export default {
  name: "mytask",
  data() {
    return {
      loading: false, //是显示加载
      title: "申请单详情",
      size: "medium",
      editFormVisible: false, //控制编辑页面显示与隐藏
      // 申请单详情对象
      details: {},
      // cookie中的基本用户信息对象
      user: {
        id: 0,
        role: "",
        store: "",
        storeid: 0,
        userName: "",
        useremail: "",
      },
      // 编辑与添加
      editForm: {
        notes: "",
        opinion: "",
      },
      // 请求数据参数
      formInline: {
        page: 1,
        size: 10,
        id: 0,
        email: "",
      },
      // 申请单状态
      states: [],
      // 数据
      applyData: [],
      // rules表单验证
      rules: {
        opinion: [
          { required: true, message: "请选择审核意见", trigger: "blur" },
        ],
      },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
      // 审核提交对象 意见,申请单id,当前用户id,当前用户邮箱,备注
      examineparame: {
        opinion: "",
        applicationid: "",
        id: 0,
        useremail: "",
        notes: "",
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
    print,
  },
  /**
   * 数据发生改变
   */
  watch: {},
  /**
   * 创建完毕
   */
  created() {
    this.user = JSON.parse(getCookie("userdata"));
    this.formInline.id = this.user.id;
    this.formInline.email = this.user.useremail;
    this.getdata(this.formInline);
    this.getSelectItem();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取申请单状态列表数据
    getSelectItem() {
      getStateItem().then((res) => {
        if (res.status == 200) {
          this.states = res.data;
          console.log(this.stores);
        } else {
          this.$message({
            type: "info",
            message: "获取申请单状态列表失败,刷新页面试试",
          });
        }
      });
    },
    // 提交审核或验收意见
    submit(opinion, index, row) {
      this.$prompt("请输入" + opinion + "备注", "提示", {
        confirmButtonText: "提交",
        cancelButtonText: "取消",
      })
        .then((value) => {
          this.examineparame.opinion = opinion;
          this.examineparame.applicationid = row.applicationid;
          this.examineparame.id = this.user.id;
          this.examineparame.useremail = this.user.useremail;
          this.examineparame.notes = value.value;
          // 提交
          examine(this.examineparame)
            .then((res) => {
              if (res.status == 200) {
                this.$message({
                  type: "success",
                  message: "已提交",
                });
                this.getdata(this.formInline);
              } else {
                this.$message({
                  type: "error",
                  message: res.statusInfo.message,
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("提交失败，请稍后重试！");
            });
        })
        .catch(() => {});
    },
    // 获取数据方法
    getdata(parameter) {
      this.loading = true;
      // 获取数据列表
      getmytask(parameter).then((res) => {
        this.loading = false;
        if (res.status == 200) {
          this.applyData = res.data;
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page;
          this.pageparm.pageSize = this.formInline.size;
          this.pageparm.total = res.count;
        } else {
          this.$message({
            type: "error",
            message: res.statusInfo.message,
          });
        }
      });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.size = parm.pageSize;
      this.getdata(this.formInline);
    },
    //搜索事件
    search() {
      this.getdata(this.formInline);
    },
    //显示详情界面
    handleEdit: function (index, row) {
      // 拿申请单号发送请求
      if (row != undefined && row != "undefined") {
        this.editFormVisible = true;
        applydetails({ applicationid: row.applicationid }).then((res) => {
          if (res.status == 200) {
            this.details = res.data;
          } else {
            this.$message({
              type: "error",
              message: res.statusInfo.message,
            });
          }
        });
      } else {
        this.$message({
          type: "error",
          message: "参数缺失",
        });
      }
    },
    // 详情提交方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          this.examineparame.opinion = this.editForm.opinion;
          this.examineparame.applicationid = this.details.applicationid;
          this.examineparame.id = this.user.id;
          this.examineparame.useremail = this.user.useremail;
          this.examineparame.notes = this.editForm.notes;
          examine(this.examineparame)
            .then((res) => {
              this.editFormVisible = false;
              this.loading = false;
              if (res.status == 200) {
                this.$message({
                  type: "success",
                  message: "提交成功",
                });
              } else {
                this.$message({
                  type: "error",
                  message: res.statusInfo.message,
                });
              }
            })
            .catch((err) => {
              this.editFormVisible = false;
              this.loading = false;
              this.$message.error("提交失败，请稍后再试！");
            });
        } else {
          return false;
        }
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == "edit") {
        this.editFormVisible = false;
      }
    },
    // 详情 转入转出仓库显示
    getinstorestr(type) {
      if (type == "转移") {
        return "转出仓库";
      } else {
        return "所属仓库";
      }
    },
    getoutstorestr(type) {
      if (type == "转移") {
        return "转出仓库";
      } else {
        return "";
      }
    },
    // 按钮禁用
    getdisabled(state) {
      if (state == "待审核") {
        return false;
      } else if (state == "待验收") {
        return false;
      } else {
        return true;
      }
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
</style>